package com.zhkj.txg.module.group.ui;

import android.app.Activity;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.zhkj.lib.base.ViewModelActivity;
import com.zhkj.lib.http.HttpResponse;
import com.zhkj.lib.http.HttpResponseStatus;
import com.zhkj.txg.R;
import com.zhkj.txg.module.group.adapter.GroupListAdapter;
import com.zhkj.txg.module.group.entity.GroupList;
import com.zhkj.txg.module.group.entity.GroupListResponse;
import com.zhkj.txg.module.group.vm.GroupListViewModel;
import com.zhkj.txg.module.home.adapter.HomeBannerAdapter;
import com.zhkj.txg.module.home.entity.BannerEntity;
import com.zhkj.txg.utils.ActionUtilsKt;
import com.youth.banner.Banner;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author
 * @date
 */
public class GroupListActivity extends ViewModelActivity<GroupListViewModel> {

    private Banner<BannerEntity, HomeBannerAdapter> banner;
    private RecyclerView recycler_view;

    private GroupListAdapter mAdapter;

    public static void startActivity(Activity context) {
        Intent intent = new Intent(context, GroupListActivity.class);
        context.startActivity(intent);
    }

    @NotNull
    @Override
    public GroupListViewModel setViewModel() {
        ViewModelProvider.AndroidViewModelFactory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication());
        return new ViewModelProvider(this, factory).get(GroupListViewModel.class);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_group_list;
    }

    @Nullable
    @Override
    public View statusTitleBar() {
        return findViewById(R.id.titleProduct);
    }

    @Override
    public void initUI() {
        recycler_view = findViewById(R.id.recycler_view);
    }

    @Override
    public void initData() {
        mAdapter = new GroupListAdapter();
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            viewModel.getGroup(false);
        });
        mAdapter.setHeaderView(createHeaderView());
        recycler_view.setAdapter(mAdapter);
        // 设置列表空布局
        mAdapter.setEmptyView(R.layout.layout_empty_data);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                GroupDetailActivity.Companion.normalProduct(GroupListActivity.this, mAdapter.getItem(position).getId());
            }
        });
        viewModel.getGroup(true);
    }

    @Override
    public void initViewModel() {
        viewModel.getGroupListLiveData().observe(this, new Observer<HttpResponse<GroupListResponse>>() {
            @Override
            public void onChanged(HttpResponse<GroupListResponse> response) {
                handlerResponseStatus(response.getStatus(), response.getStatusTip(), false);
                if (response.getStatus() == HttpResponseStatus.Status.SUCCESS) {
                    GroupListResponse result = response.getResponse();
                    if (result.getLoadMore()) {
                        mAdapter.addData(result.getData().getGoods());
                    } else {
                        mAdapter.setNewData(result.getData().getGoods());
                    }
                    if (result.getHasMore()) {
                        mAdapter.getLoadMoreModule().loadMoreComplete();
                    } else {
                        mAdapter.getLoadMoreModule().loadMoreEnd();
                    }
                    fillData(result);
                }
            }
        });
    }

    private void fillData(GroupListResponse result) {
        GroupList groupList = result.getData();
        groupList.getBanner();
        banner.setDatas(groupList.getBanner()).start();
    }

    private View createHeaderView() {
        View view = getLayoutInflater().inflate(R.layout.item_banner, null);
        banner = view.findViewById(R.id.banner);
        banner.addBannerLifecycleObserver(this).setAdapter(new HomeBannerAdapter(this)).setOnBannerListener((data, position) -> {
            HttpResponse<GroupListResponse> value = viewModel.getGroupListLiveData().getValue();
            if (value != null && value.getResponse() != null) {
                BannerEntity banner = value.getResponse().getData().getBanner().get(position);
                if (banner.getJumpType() != null)
                    ActionUtilsKt.typeJump(GroupListActivity.this, banner.getJumpType(),
                            banner.getParams(), banner.getTitle());
            }
        });
        return view;
    }
}
